/*************************************************
Author: byron
Date: 12/11/2012
Description:
Notes: 
*************************************************/

#ifndef __CUserStatus_h__
#define __CUserStatus_h__

#include "commclass.h"
#include "CTArray.h"
#include "CTHashObjectMgr.h"

class MsgAnsMapInfo;
class MsgAnsGMPlayer;

class CStatusUnit
{
public:
    enum STATUS_ID_TAG
    {
        //家族战
        STATUS_ID_CLAN_BATTLE_INSPIRE = 1005,     //鼓舞能力ID

        //怪物攻城
        STATUS_ID_ENEMY_ATK_CITY_SELF_BUFF_1 = 1006,    //学院1给自己加的BUFF
        STATUS_ID_ENEMY_ATK_CITY_SELF_BUFF_2 = 1007,    //学院2给自己加的BUFF
        STATUS_ID_ENEMY_ATK_CITY_SELF_BUFF_3 = 1008,    //学院3给自己加的BUFF
        STATUS_ID_ENEMY_ATK_CITY_SELF_BUFF_4 = 1009,    //学院4给自己加的BUFF
        STATUS_ID_ENEMY_ATK_CITY_OTHER_BUFF_1 = 1010,    //学院1给其他人加的BUFF
        STATUS_ID_ENEMY_ATK_CITY_OTHER_BUFF_2 = 1011,    //学院2给其他人加的BUFF
        STATUS_ID_ENEMY_ATK_CITY_OTHER_BUFF_3 = 1012,    //学院3给其他人加的BUFF
        STATUS_ID_ENEMY_ATK_CITY_OTHER_BUFF_4 = 1013,    //学院4给其他人加的BUFF
        
        STATUS_ID_CLAN_PASSIVE_SKILL = 1032,    //家族被动技能状态ID
        STATUS_ID_CLAN_ACTIVE_SKILL = 1033,    //家族主动技能状态ID
        STATUS_ID_CLAN_AUCTION_KING = 1034,    //家族拍卖王状态ID
    };

    CStatusUnit()
    {
        Clear();
    }

    void Clear()
    {
        _chAddNum = 0;
        _shActID = 0;
        _iResID = 0;
        _iEndTime = 0;
    }

    virtual ~CStatusUnit()
    {
    }

    bool IsValidID() const {return _iResID > 0;}
    char GetAddNum() const {return _chAddNum;}
    short GetActID() const {return _shActID;}
    int GetResID() const {return _iResID;}
    int GetEndTime() const {return _iEndTime;}

    void SetAddNum(char chAddNum){_chAddNum = chAddNum;}
    void SetActID(short shVal){_shActID = shVal;}
    void SetEndTime(int iEndTime){_iEndTime = iEndTime;}
    void SetResID(int iResID){_iResID = iResID;}

    int CalcStatusAddAttr(CPlayerAttr& stNewAttr, CPlayerAttr& stBaseAttr);

private:
    char        _chAddNum;     //叠加层数
    short       _shActID;      //活动ID
    int         _iResID;       //配置ID
    int         _iEndTime;     //失效时刻(自然时间点)
};

class CUserStatus
{
public:
    typedef CTLib::CTHashObjectMgr<int, CStatusUnit> T_USER_STATUS_HASH;

    CUserStatus()
    {
        _iLastUpdateTime = 0;
        //Hash创建就清除会导致崩溃
        //Clear();
    }

    void Clear()
    {
        //Hash清空需要逐个取出释放掉
        int iIndex = 0;
        while ((iIndex = _stUserStatusHash.GetHeadIndex()) >= 0)
        {
            _stUserStatusHash.HashFreeByIndex(iIndex);
        }
    }

    virtual ~CUserStatus()
    {
    }

    static int CountSize()
    {
        return T_USER_STATUS_HASH::CountSize(MAX_STATUS_PLAYER_COUNT);
    }

    int Init(const char* sBuff)
    {
        return _stUserStatusHash.Init(sBuff, MAX_STATUS_PLAYER_COUNT);
    }

    bool IsUserStatusEmpty() const
    {
        return (_stUserStatusHash.Size(T_USER_STATUS_HASH::USED_LIST) <= 0);
    }
    
    bool IsUserStatusFull() const
    {
        return (_stUserStatusHash.Size(T_USER_STATUS_HASH::USED_LIST) >=
                _stUserStatusHash.GetObjectCount());
    }
    
    int AddStatus(CStatusUnit& stStatusUnit);
    int FindStatus(int iResID);
    int FindStatus(int iResID, CStatusUnit& stStatusUnit);
    int DelStatus(int iResID);
    CStatusUnit& GetStatus(int iIndex);

    int CalcStatusAddAttr(CPlayerAttr& stNewAttr);

    int GMStatusList(MsgAnsGMPlayer& stAns);

    void GetUserStatus(MsgAnsMapInfo& stMsgAnsMap);
    int GetStatusList(CStatusList& stStatusList, int iActType);
    int GetLastUpdateTime() const {return _iLastUpdateTime;}
    void SetLastUpdateTime(int iVal) {_iLastUpdateTime = iVal;}

    void UpdateUserStatus(bool& bHasChg);

private:
    int _iLastUpdateTime;
    T_USER_STATUS_HASH _stUserStatusHash;
};

#endif // __CUserStatus_h__

